#!/bin/bash

. $ZMICRO_HOME/commands/machine/veth/config

remove_eth() {
  local veth_name=$1
  local veth_name_inner=${veth_name}-0
  local veth_name_outer=${veth_name}-1

  sudo ip link show ${veth_name_outer} >>/dev/null 2>&1
  if [ "$?" != "0" ]; then
    log::error "veth ${veth_name} not found."
    exit 0
  fi

  sudo ip netns exec $NETWORK_NAMESPACE ip link del ${veth_name_inner}
  log::success "veth ${veth_name} removed."
}

help() {
  echo "Usage:"
  echo "  zmachine veth create <ip>"
  echo ""
}

core() {
  if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
    help
    exit 0
  fi

  local network_ip=$1
  if [ "$network_ip" = "" ]; then
    log::error "ip is required"
    exit 1
  fi

  # local network_name="zmicro_$(string::replace $network_ip . _)"

  local last=$(string::split_last_element . $network_ip)
  local last_second=$(string::split_last_second_element . $network_ip)
  local network_name="zmicro_${last_second}${last}"

  remove_eth $network_name $network_ip
}

run() {
  core $@
}

run $@
